<template>
  <a-layout class="layout">
    <a-layout-header class="header">
      <div class="header-content">
        <h1 class="header-title">
          <CalendarOutlined /> 会议室预定系统
        </h1>
        <div class="user-actions">
          <span class="welcome-text" v-if="authStore.isAuthenticated">
            欢迎, <strong>{{ authStore.user?.username }}</strong>
          </span>
          <span class="welcome-text" v-else>
            欢迎, <strong>游客</strong>
          </span>
          <a-button v-if="authStore.isAuthenticated" @click="handleLogout">退出登录</a-button>
          <a-button v-else @click="goToLogin">登录</a-button>
        </div>
      </div>
    </a-layout-header>

    <a-layout-content class="content">
      <a-row :gutter="[20, 20]">
        <!-- Left: Booking Form -->
        <a-col :xs="24" :lg="12">
          <booking-form @booking-created="handleBookingCreated" />
        </a-col>

        <!-- Right: Bookings List -->
        <a-col :xs="24" :lg="12">
          <booking-list ref="bookingListRef" />
        </a-col>
      </a-row>
    </a-layout-content>
  </a-layout>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { message } from 'ant-design-vue'
import { CalendarOutlined } from '@ant-design/icons-vue'
import { useAuthStore } from '@/store/auth'
import BookingForm from '@/components/BookingForm.vue'
import BookingList from '@/components/BookingList.vue'

const router = useRouter()
const authStore = useAuthStore()
const bookingListRef = ref(null)

const handleLogout = () => {
  authStore.logout()
  message.success('退出成功')
  router.push('/login')
}

const goToLogin = () => {
  router.push('/login')
}

const handleBookingCreated = () => {
  // Refresh booking list when new booking is created
  if (bookingListRef.value) {
    bookingListRef.value.fetchBookings()
  }
}
</script>

<style scoped>
.layout {
  min-height: 100vh;
}

.header {
  background: #fff;
  padding: 0 20px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.header-content {
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 1400px;
  margin: 0 auto;
}

.header-title {
  color: #1890ff;
  margin: 0;
  font-size: 24px;
}

.user-actions {
  display: flex;
  gap: 15px;
  align-items: center;
}

.welcome-text {
  color: #262626;
}

.content {
  padding: 24px;
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}
</style>
